package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author spt
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}",setmeal,checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(),checkgroupId);
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(),page.getResult());
    }

    // 11_手机端_体检预约_套餐展示
    @Override
    public List<Setmeal> getSetmeal() {
        log.info("手机端_体检预约_套餐展示");
        List<Setmeal> setmealList = setMealDao.findAllSetmeal();
            return setmealList;


    }

    //13_手机端_体检预约_套餐详情查看
    @Override
    public Setmeal findById(Integer id) {
        log.info("手机端_体检预约_套餐详情查看的id{},id");
        Setmeal sl = setMealDao.findById(id);
//        根据管系表查询检查组ids
        List<Integer> checkGroupIds = setMealDao.selectCheckGroup(id);
//        根据检查组ids查询检查组
        List<CheckGroup> checkGroupList = new ArrayList<>();
        for (Integer checkGroupId : checkGroupIds) {
            CheckGroup checkGroup = checkGroupDao.selectById(checkGroupId);
//            根据检查组id查询检查项目
            List<Integer> itemIds = checkItemDao.selectGroupIteam(checkGroupId);
            List<CheckItem> checkItems = new ArrayList<>();
            for (Integer itemId : itemIds) {
                CheckItem checkItem = checkItemDao.selectById(itemId);
                checkItems.add(checkItem);
            }
            checkGroup.setCheckItems(checkItems);
            checkGroupList.add(checkGroup);
        }
        sl.setCheckGroups(checkGroupList);
        return sl;
    }
}
